/*
    * Copyright (c) 2024 Hunan OpenValley Digital Industry Development Co., Ltd.
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    *     http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
 */

#include "file_share_callback_proxy.h"
#include "jsonUtil.hpp"

namespace OHOS {
namespace FileShare {

int32_t FileShareCallbackProxy::OnSendEvent(SendState state, SendInfo &info)
{
    LOGD("OnSendEvent start.");
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    sptr<IRemoteObject> remote = Remote();
    CHKPTR(remote, FS_NULL_PARAM);

    data.WriteInt32((int32_t) state);

    nlohmann::json jsonObject;
    SendInfoToJson(info, jsonObject);
    std::string infoStr = jsonObject.dump();
    data.WriteString(infoStr);
    
    int32_t ret = remote->SendRequest(ON_SEND_EVENT, data, reply, option);
    if (ret != FS_SUCCESS) {
        LOGE("sendRequest failed, ret:%{public}d", ret);
        return FS_PROXY_SEND_REQUEST_FAILED;
    }

    return FS_SUCCESS;
}

int32_t FileShareCallbackProxy::OnShareStatusEvent(bool shareStatus)
{
    LOGD("OnShareStatusEvent start.");
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    sptr<IRemoteObject> remote = Remote();
    CHKPTR(remote, FS_NULL_PARAM);

    data.WriteBool(shareStatus);

    int32_t ret = remote->SendRequest(ON_SHARE_STATUS_EVENT, data, reply, option);
    if (ret != FS_SUCCESS) {
        LOGE("sendRequest failed, ret:%{public}d", ret);
        return FS_PROXY_SEND_REQUEST_FAILED;
    }

    return FS_SUCCESS;
}

} // namespace FileShare
} // namespace OHOS

